# -*- coding: utf-8 -*- 
"""
Project: automation-api-project
Creator: jinsheng
Create time: 2021-11-04 11:35
IDE: PyCharm
Introduction:
"""
import datetime
from time import sleep
import redis
from common.readConfig import WRConfigFile


class OperateRedis:
    def __init__(self, version=17):
        conf = WRConfigFile().read_conf
        if version == 17:
            host = conf('redis', 'host')
            port = conf('redis', 'port')
        if version == 221:
            host = conf('redis', 'host221')
            port = conf('redis', 'port221')
        if version == 156:
            host = conf('redis', 'host156')
            port = conf('redis', 'port156')
        if version == '221_n':
            host = conf('redis', 'host221_n')
            port = conf('redis', 'port221_n')
        if version == 'dxy':
            host = conf('redis', 'hostdxy')
            port = conf('redis', 'portdxy')

        self.rds = redis.StrictRedis(host=host, port=port, decode_responses=True)

    def delete(self, *args):
        """
        根据name删除redis中的任意数据类型
        """
        self.rds.delete(*args)

    def exists(self, *args):
        """
        检测redis的name是否存在
        """
        return self.rds.exists(*args)

    def get_name(self, pattern):
        """
        根据* ？等通配符匹配获取redis的name
        """
        return self.rds.keys(pattern=pattern)

    def get_type(self, name):
        """
        获取name对应值的类型
        """
        return self.rds.type(name)

    def get_total_counts(self):
        return self.rds.dbsize()


class RedisString(OperateRedis):
    def __init__(self, version=17):
        super().__init__(version)

    def get_value(self, name):
        """
        获取name对应的值
        """
        return self.rds.get(name)

    def get_values(self, *args):
        """
        批量获取值
        """
        return self.rds.mget(*args)

    def insert(self, name, value):
        self.rds.set(name, value)


class RedisHash(OperateRedis):
    def __init__(self, version=17):
        super().__init__(version)

    def get_value(self, name, key):
        """
        根据name可key获取value值
        """
        return self.rds.hget(name, key)

    def get_key_value(self, name):
        """
        根据name获取键值对
        """
        return self.rds.hgetall(name)

    def insert_key_value(self, name, key, value):
        self.rds.hset(name, key, value)


class RedisSet(OperateRedis):
    def __init__(self, version=17):
        super().__init__(version)

    def get_members(self, name):
        """
        根据name获取集合内所有成员
        @param name: 集合的name
        @return:
        """
        return self.rds.smembers(name)

    def get_count_of_members(self, name):
        """
        根据name获取集合内元素个数
        @param name: 集合名称
        @return:
        """
        return self.rds.scard(name)

    def is_member_of(self, name, value):
        """
        判断value是否是name集合中的元素
        @param name: 集合名称
        @param value: 元素名称
        @return:
        """
        return self.rds.sismember(name, value)


if __name__ == '__main__':
    st = OperateRedis(version=221)
    res = st.get_name('DEVICE_CENTER:DEVICE_CODE:*')
    print(len(res))
    # for name in res:
    #     print(name)
    # print(st.get_type('UNIT_CENTER:ceshi:77402992076140544'))
    # h = RedisHash(version=17)
    # # h = RedisString(version=17)
    # # print(h.get_value('UNIT_CENTER:ceshi:77402990465527808'))
    # # print(h.get_type('MODELVIEW:MODEL:ods_device_ts@ods_device_electric_ts'))
    # print(h.get_key_value('DEVICE_CENTER:collect:102726741115543552'))
    # print(h.get_value('DEVICE_CENTER:test_infoa:120543279782567936', 'test_bb'))
    # h.insert_key_value('DEVICE_CENTER:test_infoa:120543279782567936', 'age', 20)
    # print(st.get_total_counts())
